<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="130px"
    >
      <!-- <el-form-item label="换货单id" prop="exchangeId">
      <el-input v-model="dataForm.exchangeId" placeholder="换货单id"></el-input>
    </el-form-item> -->
      <!-- <el-form-item label="需换干货id" prop="spuId">
        <el-input v-model="dataForm.spuId" placeholder="需换干货id"></el-input>
      </el-form-item> -->
      <el-form-item label="需换干货名称" prop="spuId">
        <category-cascader-spu
          :catelogPathWithSpu.sync="catelogPathWithSpu"
        ></category-cascader-spu>
      </el-form-item>
      <!-- <el-form-item label="需换干货数量" prop="spuNum">
        <el-input
          v-model="dataForm.spuNum"
          placeholder="需换干货数量"
        ></el-input>
      </el-form-item> -->
      <el-form-item label="仓库" prop="wareId">
        <el-select v-model="dataForm.wareId" placeholder="请选择仓库" clearable>
          <el-option
            :label="w.name"
            :value="w.id"
            v-for="w in wareList"
            :key="w.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="需换干货总金额" prop="oriSpuTotalPrice">
        <el-input
          v-model="dataForm.oriSpuTotalPrice"
          placeholder="需换干货总金额"
        ></el-input>
      </el-form-item>
      <el-form-item label="需换干货单价" prop="oriSpuPrice">
        <el-input
          v-model="dataForm.oriSpuPrice"
          placeholder="需换干货单价"
        ></el-input>
      </el-form-item>
      <el-form-item label="置换后干货id" prop="afterSpuId">
        <el-input
          v-model="dataForm.afterSpuId"
          placeholder="置换后干货id"
        ></el-input>
      </el-form-item>
      <el-form-item label="置换后干货数量" prop="afterSpuNum">
        <el-input
          v-model="dataForm.afterSpuNum"
          placeholder="置换后干货数量"
        ></el-input>
      </el-form-item>
      <el-form-item label="置换后干货总金额" prop="afterSpuTotalPrice">
        <el-input
          v-model="dataForm.afterSpuTotalPrice"
          placeholder="置换后干货总金额"
        ></el-input>
      </el-form-item>
      <el-form-item label="置换后干货单价" prop="afterSpuPrice">
        <el-input
          v-model="dataForm.afterSpuPrice"
          placeholder="置换后干货单价"
        ></el-input>
      </el-form-item>
      <el-form-item label="供应商id" prop="supplierId">
        <el-input
          v-model="dataForm.supplierId"
          placeholder="供应商id"
        ></el-input>
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="dataForm.supplierName"
          placeholder="供应商名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-input v-model="dataForm.status" placeholder="状态"></el-input>
      </el-form-item>
      <el-form-item label="仓库id" prop="wareId">
        <el-input v-model="dataForm.wareId" placeholder="仓库id"></el-input>
      </el-form-item>
      <el-form-item label="需换干货批次" prop="oriBatch">
        <el-input
          v-model="dataForm.oriBatch"
          placeholder="需换干货批次"
        ></el-input>
      </el-form-item>
      <el-form-item label="置换后干货批次" prop="afterBatch">
        <el-input
          v-model="dataForm.afterBatch"
          placeholder="置换后干货批次"
        ></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-input
          v-model="dataForm.updateTime"
          placeholder="更新时间"
        ></el-input>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import CategoryCascaderSpu from "../common/category-cascader-spu";
export default {
  components: { CategoryCascaderSpu },
  watch: {
    visible(val) {
      this.catelogPathWithSpu = [];
    }
  },
  data() {
    return {
      visible: false,
      catPathWithSpuSub: null,
      catelogPathWithSpu: [],
      wareList: [],
      dataForm: {
        id: 0,
        exchangeId: "",
        spuId: "",
        spuName: "",
        spuNum: "",
        oriSpuTotalPrice: "",
        oriSpuPrice: "",
        afterSpuId: "",
        afterSpuNum: "",
        afterSpuTotalPrice: "",
        afterSpuPrice: "",
        supplierId: "",
        supplierName: "",
        status: "",
        wareId: "",
        wareName: "",
        oriBatch: "",
        afterBatch: "",
        updateTime: ""
      },
      dataRule: {
        exchangeId: [
          { required: true, message: "换货单id不能为空", trigger: "blur" }
        ],
        spuId: [
          { required: true, message: "需换干货id不能为空", trigger: "blur" }
        ],
        spuNum: [
          { required: true, message: "需换干货数量不能为空", trigger: "blur" }
        ],
        oriSpuTotalPrice: [
          { required: true, message: "需换干货总金额不能为空", trigger: "blur" }
        ],
        oriSpuPrice: [
          { required: true, message: "需换干货单价不能为空", trigger: "blur" }
        ],
        afterSpuId: [
          { required: true, message: "置换后干货id不能为空", trigger: "blur" }
        ],
        afterSpuNum: [
          { required: true, message: "置换后干货数量不能为空", trigger: "blur" }
        ],
        afterSpuTotalPrice: [
          {
            required: true,
            message: "置换后干货总金额不能为空",
            trigger: "blur"
          }
        ],
        afterSpuPrice: [
          { required: true, message: "置换后干货单价不能为空", trigger: "blur" }
        ],
        supplierId: [
          { required: true, message: "供应商id不能为空", trigger: "blur" }
        ],
        supplierName: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        wareId: [
          { required: true, message: "仓库id不能为空", trigger: "blur" }
        ],
        oriBatch: [
          { required: true, message: "需换干货批次不能为空", trigger: "blur" }
        ],
        afterBatch: [
          { required: true, message: "置换后干货批次不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getWares();
  },
  mounted() {
    this.catPathWithSpuSub = PubSub.subscribe("catPath", (msg, val) => {
      this.dataForm.spuId = val[val.length - 1];
    });
  },
  methods: {
    getWares() {
      this.$http({
        url: this.$http.adornUrl("/ware/wareinfo/list"),
        method: "get",
        params: this.$http.adornParams({
          page: 1,
          limit: 500
        })
      }).then(({ data }) => {
        this.wareList = data.page.list;
      });
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/ware/exchangedetail/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.exchangeId = data.exchangeDetail.exchangeId;
              this.dataForm.spuId = data.exchangeDetail.spuId;
              this.dataForm.spuNum = data.exchangeDetail.spuNum;
              this.dataForm.oriSpuTotalPrice =
                data.exchangeDetail.oriSpuTotalPrice;
              this.dataForm.oriSpuPrice = data.exchangeDetail.oriSpuPrice;
              this.dataForm.afterSpuId = data.exchangeDetail.afterSpuId;
              this.dataForm.afterSpuNum = data.exchangeDetail.afterSpuNum;
              this.dataForm.afterSpuTotalPrice =
                data.exchangeDetail.afterSpuTotalPrice;
              this.dataForm.afterSpuPrice = data.exchangeDetail.afterSpuPrice;
              this.dataForm.supplierId = data.exchangeDetail.supplierId;
              this.dataForm.supplierName = data.exchangeDetail.supplierName;
              this.dataForm.status = data.exchangeDetail.status;
              this.dataForm.wareId = data.exchangeDetail.wareId;
              this.dataForm.oriBatch = data.exchangeDetail.oriBatch;
              this.dataForm.afterBatch = data.exchangeDetail.afterBatch;
              this.dataForm.updateTime = data.exchangeDetail.updateTime;
              this.catelogPathWithSpu = data.exchangeDetail.catelogPathWithSpu;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      console.log(this.dataForm.wareId);
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/ware/exchangedetail/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              exchangeId: this.dataForm.exchangeId,
              spuId: this.dataForm.spuId,
              spuNum: this.dataForm.spuNum,
              oriSpuTotalPrice: this.dataForm.oriSpuTotalPrice,
              oriSpuPrice: this.dataForm.oriSpuPrice,
              afterSpuId: this.dataForm.afterSpuId,
              afterSpuNum: this.dataForm.afterSpuNum,
              afterSpuTotalPrice: this.dataForm.afterSpuTotalPrice,
              afterSpuPrice: this.dataForm.afterSpuPrice,
              supplierId: this.dataForm.supplierId,
              supplierName: this.dataForm.supplierName,
              status: this.dataForm.status,
              wareId: this.dataForm.wareId,
              oriBatch: this.dataForm.oriBatch,
              afterBatch: this.dataForm.afterBatch,
              updateTime: this.dataForm.updateTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
